﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.LeaveCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.LeaveHandler
{
    public class QueryNameHandler : IRequestHandler<QueryNameCommand, APIResult<string>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IMapper mapper;

        public QueryNameHandler(IBaseRepository<User> userRep,IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }

        public Task<APIResult<string>> Handle(QueryNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            result.Code = APIEnum.Success;
            result.Msg = "显示成功";
            //判断用户是领导/员工
            var u2 = userRep.GetAll().Where(x => x.UserId == request.UserId && x.PowerId == (int)PowerId.部门领导人).ToList();
            if (u2.Count() > 0)
            {
                var Names = userRep.GetAll().Where(x => x.PowerId == (int)PowerId.总负责人).Select(x => x.Name).ToList();
                result.Data = string.Join(",", Names);
            }
            else
            {
                //否
                //通过部门Id和领导人查询到所有领导人的Ids
                var u = userRep.GetAll().Where(x => x.DepartmentId == request.DepartmentId && x.PowerId == (int)PowerId.部门领导人);
                var LNames = u.Select(x => x.Name).ToList();
                result.Data = string.Join(",", LNames);
            }
            return Task.FromResult(result);
        }
    }
}
